package com.zyh.scs.admin.controller.jd;


import com.zyh.scs.admin.api.bo.JDAuthDto;
import com.zyh.scs.admin.api.bo.JDCashierDto;
import com.zyh.scs.admin.base.open.LaoSheResponse;
import com.zyh.scs.admin.service.jd.JDAuthService;
import com.zyh.scs.admin.service.jd.JDCashierService;
import com.zyh.scs.admin.service.jd.JDService;
import com.zyh.scs.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * 京东锦礼
 */
@Controller
@RequestMapping("/v1.0/api/open/jd")
public class JDController {

    @Autowired
    JDAuthService jdAuthService;

    @Autowired
    JDCashierService jdCashierService;

    @Autowired
    JDService jdService;

    /**
     * 授权登录
     * -- 免登录接口
     *
     * @param uid
     * @param returnType
     * @param skuId
     * @param request
     * @param response
     */
    @GetMapping(value = "/auth")
    public void auth(@RequestParam(name = "uid") String uid, @RequestParam(name = "returnType", required = false) String returnType,
                     @RequestParam(name = "skuId", required = false) String skuId,
                     HttpServletRequest request, HttpServletResponse response) {

        JDAuthDto dto = new JDAuthDto();

        dto.setUid(uid);
        dto.setReturnType(returnType);
        dto.setSkuId(skuId);

        jdAuthService.authRedirect(dto, request, response);
    }

    /**
     * 接收下单通知（跳转商户收银台）
     *
     * @param request
     * @param response
     */
    @PostMapping(value = "/cashier", produces = "application/x-www-form-urlencoded;charset=UTF-8")
    public LaoSheResponse<String> cashier(@RequestParam(name = "uid") String uid,
                                          @RequestParam(name = "submitOrderTime") String submitOrderTime,
                                          @RequestParam(name = "orderId") String orderId,
                                          @RequestParam(name = "totalMoney") String totalMoney,
                                          @RequestParam(name = "freight") String freight,
                                          @RequestParam(name = "sign") String sign,
                                          HttpServletRequest request, HttpServletResponse response) {
        JDCashierDto dto = new JDCashierDto();
        dto.setUid(uid);
        dto.setSubmitOrderTime(submitOrderTime);
        dto.setOrderId(orderId);
        dto.setTotalMoney(totalMoney);
        dto.setFreight(freight);
        dto.setSign(sign);
        return jdCashierService.cashier(dto, request, response);
    }


    /**
     * 京东 订单确认支付接口
     *
     * @return
     */
    @PostMapping(value = "/sync/order/status/toJd")
    @Operation(summary = "京东 订单确认支付接口", description = "京东 订单确认支付接口")
    public R syncOrderStatusToJd() {
        return R.ok(jdService.syncOrderStatusToJd());
    }

    /**
     * 京东 查询退款订单
     *
     * @return
     */
    @PostMapping(value = "/query/refund/order")
    @Operation(summary = "京东 查询退款订单", description = "京东 查询退款订单")
    public R queryRefundOrder() {
        return R.ok(jdService.queryRefundOrder());
    }

    @PostMapping("notify")
    public void notify(@RequestBody String body) {
        System.out.println("接受京东退款通知：" + body);
    }
}